#!/usr/bin/env python

import cgi
import wsgiref.handlers
import os
from google.appengine.api import users
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app

from google.appengine.api.labs import taskqueue
from google.appengine.api import urlfetch
import xml.etree.ElementTree as ET

from google.appengine.api import memcache

from model.db_dou_people import DbDouPeople
import logging

class ControlDouInit(webapp.RequestHandler):
    def get(self):
        dbdoupeople = DbDouPeople()
        dbdoupeople.db_uid = 'samhooxee'
        dbdoupeople.id_from = 'origin'
        dbdoupeople.flag_contact = False
        dbdoupeople.put()
        self.response.out.write("ControlDouInit done!!!")

class ControlDouGet(webapp.RequestHandler):
    def get(self):
        dbdoupeople_query = DbDouPeople.all()
        dbdoupeople_query.filter('flag_contact != ', True)
        cnt = 0
        for people in dbdoupeople_query:
            if cnt > 1000:
                break
            db_uid = people.db_uid
            people.flag_contact = True
            people.put()
            url = "http://api.douban.com/people/" + db_uid + "/contacts"
            result = urlfetch.fetch(url=url)
            result_xml = ET.fromstring(result.content)
            total = result_xml.find('{http://a9.com/-/spec/opensearchrss/1.0/}totalResults')
            total_int = int(total.text)
            page_int = total_int/44
            if total_int%44:
                page_int += 1
            for idx in range(page_int):
                idx_str = "%d" % (idx*44+1)
                url = "http://api.douban.com/people/" + db_uid + "/contacts?start-index=" + idx_str + "&max-results=44"
                #logging.info('url %s' % url)
                logging.info('db_uid %s %s' % (db_uid, idx_str))
                result = urlfetch.fetch(url=url)
                result_xml = ET.fromstring(result.content)
                items = result_xml.findall('{http://www.w3.org/2005/Atom}entry')
                qd = DbDouPeople.all()
                try :
                    for item in items:
                        c_db_uid = item.find('{http://www.douban.com/xmlns/}uid').text
                        #should check c_db_uid in database
                        qdft = qd.filter('db_uid = ', c_db_uid)
                        #logging.info('db_uid %s' % c_db_uid)
                        if qdft.count() == 0:
                            c_title = item.find('{http://www.w3.org/2005/Atom}title').text
                            dbdoupeople = DbDouPeople()
                            dbdoupeople.db_uid = c_db_uid
                            dbdoupeople.id_from = db_uid
                            dbdoupeople.flag_contact = False
                            dbdoupeople.title = c_title
                            dbdoupeople.put()
                            cnt += 1
                except:
                    self.response.out.write("ControlDouGet error!!!")
        self.response.out.write("ControlDouGet done!!!")
        logging.info('cnt %d' % cnt)

def main():
  application = webapp.WSGIApplication([('/controlDouInit', ControlDouInit),
                                        ('/controlDouGet', ControlDouGet),
                                        ], debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
  main()